class Participant {
    name = null
    videoId = null
    audioId = null
    muted = null
    speaking = false
    muteVideo = false
    deptName = null
    deptId = null
    roomName = null
    isVcs = false
    isTemp = false
    isGrade = false
    number = null
    type = null
    status = ''
    gradeNumber = null
    gradeName = null
    net = null
    icon = null
    meetingIcon = null
    order = 1
    isRtsp = false
    realName = null
    isControl = false

    constructor(name, roomName, number, type, isVcs, gradeNumber, isControl) {
        this.name = name
        this.realName = name
        this.roomName = roomName
        this.number = number
        this.type = type ? Number(type) : null
        this.isVcs = isVcs
        this.videoId = `video-${name}`
        this.audioId = `audio-${name}`
        this.muted = false
        this.muteVideo = false
        this.speaking = false
        this.gradeNumber = gradeNumber
        if (this.gradeNumber) {
            this.isGrade = true
        }
        this.isControl = isControl
        Object.defineProperty(this, 'rtcPeer', {writable: true})
        Object.defineProperty(this, 'speakHark', {writable: true})
    }

    setStatus(status) {
        this.status = status
        let st = this.status.toLowerCase()
        if (st !== 'talking') {
            this.meetingIcon = ''
        }
        switch (st) {
            case 'talking':
                this.order = 0
                break
            case 'broading':
                this.order = 1
                break
            case 'idle':
                this.order = 2
                break
            case 'offline':
                this.order = 3
                break
            case 'unknow':
                this.order = 4
                if (this.type == '4') {
                    this.isRtsp = true
                }
                break
        }
    }

    setMute(mute) {
        this.muted = mute
        if (this.muted) {
            if (this.meetingIcon) {
                this.meetingIcon = 'conf_mute'
            }
        } else {
            if (this.meetingIcon) {
                this.meetingIcon = 'conf'
            }
        }
    }

    setDept(deptId, deptName) {
        this.deptId = deptId
        this.deptName = deptName
    }

    getVideoElement() {
        return document.getElementById(this.videoId)
    }

    getAudioElement() {
        return document.getElementById(this.audioId)
    }

    offerToReceiveVideo(error, sdp, wp) {
        if (error) return console.error('sdp offer error',error)
        window.vcsSocket.prototype.receive(this.type == '1' ? this.number : this.name, sdp)
    }

    onIceCandidate(candidate, wp) {
        window.vcsSocket.prototype.iceCandidate(candidate, this.type == '1' ? this.number : this.name)
    }

    dispose() {
        if (this.rtcPeer) {
            this.rtcPeer.dispose()
            this.rtcPeer = null
        }
        if (this.speakHark) {
            this.speakHark.stop()
            this.speakHark = null
        }
    }
}

export default Participant